﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace Framework.Data.Utilities
{
    public class TextFormatter : IFormatProvider, ICustomFormatter
    {

        #region "Constantes"
        public const string TELEPHONE = "(000) 000-0000";
        public const string CODE_POSTAL = "A0A 0A0";
        #endregion
        public const string POURCENT = "%";

        #region "Membres"
        #endregion
        protected string _formatString = string.Empty;

        #region "Constructeurs"

        public TextFormatter(string formatString)
        {
            this._formatString = formatString;
        }

        #endregion

        #region "Propriétés"
        public string FormatString
        {
            get { return this._formatString; }
            set { this._formatString = value; }
        }

        #endregion

        #region "Méthodes"

        public string Format(string formatString, object arg, System.IFormatProvider formatProvider)
        {
            if (this._formatString == null)
            {
                if (arg is IFormattable)
                {
                    return ((IFormattable)arg).ToString(formatString, formatProvider);
                }
                else
                {
                    return arg.ToString();
                }
            }

            //Mon formatage
            if (arg != null)
            {
                if (this._formatString.Equals(TELEPHONE, StringComparison.InvariantCultureIgnoreCase))
                {
                    double telephoneNumber = 0;
                    try
                    {
                        telephoneNumber = double.Parse(arg.ToString());
                        return telephoneNumber.ToString(TELEPHONE);
                    }
                    catch (Exception ex)
                    {
                        return arg.ToString();
                    }
                }
                else if (this._formatString.Equals(CODE_POSTAL, StringComparison.InvariantCultureIgnoreCase))
                {
                    Regex regx = new Regex("([A-Za-z]\\d[A-Za-z]\\d[A-Za-z]\\d)");
                    Match match = null;
                    match = regx.Match(arg.ToString());
                    if (match.Success)
                    {
                        return match.Value.Substring(0, 3).ToUpper() + " " + match.Value.Substring(3, 3).ToUpper();
                    }
                }
                else if (this._formatString.Equals(POURCENT, StringComparison.InvariantCultureIgnoreCase))
                {
                    return string.Format("{0:0.00} %", arg);
                }

                return arg.ToString();
            }
            else
            {
                return string.Empty;
            }

        }

        public object GetFormat(System.Type formatType)
        {
            if (object.ReferenceEquals(formatType, typeof(ICustomFormatter)))
            {
                return this;
            }
            else
            {
                return null;
            }

        }

        #endregion
    }
}